<!--
  Copyright © 2015 Cask Data, Inc.

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy of
  the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  License for the specific language governing permissions and limitations under
  the License.
-->

<div class="side-panel top clearfix">
  <h1 class="pull-left">My Pipelines</h1>
  <ul ng-if="ListController.pipelineList.length > 0" class="list-unstyled list-inline pull-left">
    <li>
      <span>{{ ListController.pipelineList.length }}</span>
      <span>Total</span>
    </li>
    <li>
      <i class="fa fa-circle text-success"></i>
      <span>
        {{ ListController.statusCount.running }}
      </span>
      <span>Running</span>
    </li>
    <li>
      <i class="fa fa-circle text-success"></i>
      <span>
        {{ ListController.statusCount.scheduled }}
      </span>
      <span>Scheduled</span>
    </li>
    <li>
      <i class="fa fa-circle text-danger"></i>
      <span>
        {{ ListController.statusCount.suspended }}
      </span>
      <span>Suspended</span>
    </li>
    <li>
      <i class="fa fa-circle text-muted"></i>
      <span>
        {{ ListController.statusCount.draft }}
      </span>
      <span>Drafts</span>
    </li>
  </ul>
  <a ui-sref="hydratorplusplus.create({ namespace: $stateParams.namespace })" class="btn btn-primary add-pipeline">
    <span class="fa fa-plus"></span>
  </a>
</div>

<div class="my-pipelines">
  <div ng-if="ListController.pipelineList.length > 0">

    <div class="table-responsive">
      <table class="table" cask-sortable>
        <thead>
          <tr ng-class="{'sort-enabled': ListController.pipelineList.length>0}">
            <th data-predicate="name">Pipeline Name</th>
            <th data-predicate="artifact.name">Type</th>
            <th data-predicate="_status">Status</th>
            <th data-predicate="_stats.numRuns">Total Runs</th>
            <th data-predicate="_stats.lastStartTime"
                  data-predicate-default>Last Start Time</th>
            <th data-predicate="_latest.end">Last Finished Run</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="pipeline in ListController.filtered = (ListController.pipelineList | filter: ListController.searchText ) | orderBy:sortable.predicate:sortable.reverse | myPaginate:ListController.currentPage">
            <td>
              <a ui-sref="hydratorplusplus.detail({ pipelineId: pipeline.name })" ng-if="!pipeline.isDraft">
                {{pipeline.name}}
              </a>
              <a ui-sref="hydratorplusplus.create({ draftId: pipeline.id, type:pipeline.artifact.name })" ng-if="pipeline.isDraft">
                {{pipeline.name}}
              </a>
            </td>
            <td>
              <span class="icon-{{ ListController.GLOBALS.iconArtifact[pipeline.artifact.name] }}"></span>
              <span>{{ ListController.GLOBALS.artifactConvert[pipeline.artifact.name] }}</span>
            </td>
            <td>
              <span class="status-bubble"
                ng-class="{'status-green': pipeline._status === 'Running' ||
                pipeline._status === 'Scheduled',
                'status-red': pipeline._status !== 'Running' && pipeline._status !== 'Scheduled',
                'status-grey': pipeline._status === 'Draft' }"></span>
              <span>{{ pipeline._status }}</span>
            </td>
            <td>
              <span ng-if="pipeline._stats.numRuns !== 'N/A'">{{ pipeline._stats.numRuns }}</span>
              <span ng-if="pipeline._stats.numRuns === 'N/A'"> &mdash; </span>
            </td>
            <td>
              <span ng-if="pipeline._stats.lastStartTime === 'N/A'"> &mdash; </span>
              <span ng-if="pipeline._stats.lastStartTime !== 'N/A'">{{ pipeline._stats.lastStartTime * 1000 | amDateFormat: 'MM/DD/YY h:mm:ss A' }}</span>
            </td>
            <td>
              <span ng-if="pipeline._latest">
                <strong ng-class="{ 'text-success': pipeline._latest.status === 'COMPLETED',
                  'text-danger': pipeline._latest.status === 'KILLED' || pipeline._latest.status === 'FAILED' }">{{ pipeline._latest.status | uppercase }}</strong>
                  {{ pipeline._latest.end * 1000 | amDateFormat: 'MM/DD/YY h:mm:ss A'}}
              </span>
              <span ng-if="!pipeline._latest">
                &mdash;
              </span>
            </td>

            <td>
              <span class="fa fa-trash text-danger delete-draft"
                ng-if="pipeline.isDraft"
                ng-click="caskConfirm()"
                cask-confirmable="ListController.deleteDraft(pipeline.id)"
                data-confirmable-content="Are you sure you want to delete this draft?"
              ></span>
              <span class="fa fa-trash text-danger delete-draft"
                ng-if="!pipeline.isDraft"
                ng-click="caskConfirm()"
                cask-confirmable="ListController.deleteApp(pipeline.name)"
                data-confirmable-content="Are you sure you want to delete this pipeline?"
              ></span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <div class="text-center" ng-if="ListController.filtered.length > 10">
      <uib-pagination total-items="ListController.filtered.length" ng-model="ListController.currentPage"></uib-pagination>
    </div>

  </div>

  <div ng-if="ListController.pipelineList.length === 0">
    <div class="well well-lg text-center">
      <h4>You haven't published any pipelines yet - start by adding one.</h4>
    </div>
  </div>
</div>
